From f51299a7e2f93ea859b6518fb3618cf30cb561f0 Mon Sep 17 00:00:00 2001 From: robertlipe Date: Mon, 29 Jul 2013 00:23:34 +0000 Subject: [PATCH] Clean up some of the more mechanical uses of time_t and implicit conversions. --- gpsbabel/arcdist.cc | 6 +-- gpsbabel/csv_util.cc | 59 +++++------------------------ gpsbabel/garmin.cc | 7 ++-- gpsbabel/garmin_fit.cc | 2 +- gpsbabel/lowranceusr4.cc | 2 +- gpsbabel/maggeo.cc | 81 +++++++++++----------------------------- gpsbabel/mapasia.cc | 45 ++++++++++------------ gpsbabel/mmo.cc | 10 ++--- gpsbabel/pocketfms_bc.cc | 2 +- gpsbabel/smplrout.cc | 2 +- gpsbabel/waypt.cc | 2 +- 11 files changed, 67 insertions(+), 151 deletions(-) diff --git a/gpsbabel/arcdist.cc b/gpsbabel/arcdist.cc index 945f29f5b..bfb4cea00 100644 --- a/gpsbabel/arcdist.cc +++ b/gpsbabel/arcdist.cc @@ -234,14 +234,14 @@ arcdist_process(void) (ptsopt || (ed->arcpt1->GetCreationTime().isValid()))) { /* Interpolate time */ if (ptsopt) { - wp->creation_time = ed->arcpt2->creation_time; + wp->SetCreationTime(ed->arcpt2->GetCreationTime());; } else { // Apply the multiplier to the difference between the times // of the two points. Add that to the first for the // interpolated time. int scaled_time = ed->frac * - ed->arcpt1->creation_time.msecsTo(ed->arcpt2->creation_time); - QDateTime new_time(ed->arcpt1->creation_time.addMSecs(scaled_time)); + ed->arcpt1->GetCreationTime().msecsTo(ed->arcpt2->GetCreationTime()); + QDateTime new_time(ed->arcpt1->GetCreationTime().addMSecs(scaled_time)); wp->SetCreationTime(new_time); } } diff --git a/gpsbabel/csv_util.cc b/gpsbabel/csv_util.cc index 0adcba66c..051d4619c 100644 --- a/gpsbabel/csv_util.cc +++ b/gpsbabel/csv_util.cc @@ -831,29 +831,14 @@ xcsv_epilogue_add(char* epilogue) } static -time_t +QDateTime yyyymmdd_to_time(const char* s) { - int t = atol(s); - struct tm tm; - - memset(&tm, 0, sizeof(tm)); - - tm.tm_mday = t % 100; - t = t / 100; - tm.tm_mon = t % 100 - 1; - t = t / 100; - tm.tm_year = t - 1900; - - if (mkgmtime(&tm) > 0) { - return mktime(&tm); - } else { - return 0; - } + QDate d = QDate::fromString(s, "yyyyMMdd"); + return QDateTime(d); } - /* * sscanftime - Parse a date buffer using strftime format */ @@ -924,26 +909,6 @@ writetime(char* buff, size_t bufsize, const char* format, time_t t, bool gmt) return strftime(buff, bufsize, format, stmp); } -#if 0 -/* not used */ -static -int -writeisotime(char* buff, size_t bufsize, const char* format, time_t t) -{ - static struct tm* stmp; - char* ibuff = NULL; - int i; - - ibuff = xmalloc(bufsize); - stmp = gmtime(&t); - strftime(ibuff, bufsize, format, stmp); - i = snprintf(buff, bufsize, format, ibuff); - xfree(ibuff); - return i; -} -#endif - - static int writehms(char* buff, size_t bufsize, const char* format, time_t t, int gmt) @@ -968,16 +933,10 @@ writehms(char* buff, size_t bufsize, const char* format, time_t t, int gmt) static long -time_to_yyyymmdd(time_t t) +time_to_yyyymmdd(QDateTime t) { - long b; - struct tm* tm = gmtime(&t); - - b = (1900 + tm->tm_year) * 10000 + - (1 + tm->tm_mon) * 100 + - tm->tm_mday; - - return b; + QDate d = t.date(); + return d.year() * 10000 + d.month() * 100 + d.day(); } static garmin_fs_t* @@ -1218,7 +1177,7 @@ xcsv_parse_val(const char* s, waypoint* wpt, const field_map_t* fmp, break; case XT_ISO_TIME: case XT_ISO_TIME_MS: - wpt->creation_time = xml_parse_time(s); + wpt->SetCreationTime(xml_parse_time(s)); break; case XT_NET_TIME: { time_t tt = wpt->GetCreationTime(); @@ -1939,13 +1898,13 @@ xcsv_waypt_pr(const waypoint* wpt) break; case XT_TIMET_TIME: /* time as a time_t variable */ { - time_t tt = wpt->GetCreationTime(); + time_t tt = wpt->GetCreationTime().toTime_t(); writebuff(buff, fmp->printfc, tt); } break; case XT_TIMET_TIME_MS: { /* time as a time_t variable in milliseconds */ char tbuf[24]; - writetime(tbuf, sizeof(tbuf), "%s", wpt->GetCreationTime(), false); + writetime(tbuf, sizeof(tbuf), "%s", wpt->GetCreationTime().toTime_t(), false); char mbuf[32]; snprintf(mbuf, sizeof(mbuf), "%s%03d", tbuf, wpt->GetCreationTime().time().msec()); writebuff(buff, "%s", mbuf); diff --git a/gpsbabel/garmin.cc b/gpsbabel/garmin.cc index 30724c66b..eab301650 100644 --- a/gpsbabel/garmin.cc +++ b/gpsbabel/garmin.cc @@ -983,8 +983,9 @@ waypoint_prepare(void) } else { tx_waylist[i]->alt = wpt->altitude; } - if (wpt->creation_time) { - tx_waylist[i]->time = wpt->GetCreationTime(); + gpsbabel::DateTime t = wpt->GetCreationTime(); + if (t.isValid()) { + tx_waylist[i]->time = t.toTime_t(); tx_waylist[i]->time_populated = 1; } if (category) { @@ -1135,7 +1136,7 @@ track_waypt_pr(const waypoint* wpt) (*cur_tx_tracklist_entry)->lat = wpt->latitude; (*cur_tx_tracklist_entry)->lon = wpt->longitude; (*cur_tx_tracklist_entry)->alt = (wpt->altitude != unknown_alt) ? wpt->altitude : 1e25; - (*cur_tx_tracklist_entry)->Time = wpt->creation_time; + (*cur_tx_tracklist_entry)->Time = wpt->GetCreationTime().toTime_t();; if (wpt->shortname) { strncpy((*cur_tx_tracklist_entry)->trk_ident, wpt->shortname, sizeof((*cur_tx_tracklist_entry)->trk_ident)); (*cur_tx_tracklist_entry)->trk_ident[sizeof((*cur_tx_tracklist_entry)->trk_ident)-1] = 0; diff --git a/gpsbabel/garmin_fit.cc b/gpsbabel/garmin_fit.cc index 14bc02baa..4e72886f8 100644 --- a/gpsbabel/garmin_fit.cc +++ b/gpsbabel/garmin_fit.cc @@ -376,7 +376,7 @@ fit_parse_data(fit_message_def* def, int time_offset) if (alt != 0xffff) { waypt->altitude = (alt / 5.0) - 500; } - waypt->creation_time = timestamp + 631065600; + waypt->SetCreationTime(QDateTime::fromTime_t(timestamp + 631065600)); if (speed != 0xffff) { WAYPT_SET(waypt, speed, speed / 1000.0f); } diff --git a/gpsbabel/lowranceusr4.cc b/gpsbabel/lowranceusr4.cc index db8ccfaa3..8fd978177 100644 --- a/gpsbabel/lowranceusr4.cc +++ b/gpsbabel/lowranceusr4.cc @@ -689,7 +689,7 @@ lowranceusr4_parse_trails(void) gbfgetc(file_in); /* POSIX timestamp */ - wpt_tmp->creation_time = gbfgetint32(file_in); + wpt_tmp->SetCreationTime(QDateTime::fromTime_t(gbfgetint32(file_in))); /* Long/Lat */ wpt_tmp->longitude = gbfgetdbl(file_in) / DEGREESTORADIANS; /* rad to deg */ diff --git a/gpsbabel/maggeo.cc b/gpsbabel/maggeo.cc index a3b7f43da..d4623acd3 100644 --- a/gpsbabel/maggeo.cc +++ b/gpsbabel/maggeo.cc @@ -36,7 +36,7 @@ static gbfile* maggeofile_in; static gbfile* maggeofile_out; static short_handle desc_handle = NULL; -static time_t maggeo_parsedate(char* dmy); +static QDateTime maggeo_parsedate(char* dmy); static void maggeo_writemsg(const char* const buf) @@ -173,29 +173,16 @@ maggeo_read(void) } -/* - * Note: returns allocated buffer that must be freed by caller. - */ static -char* -maggeo_fmtdate(time_t t) +QString +maggeo_fmtdate(QDateTime dt) { -#define SZ 16 - - struct tm* tm = NULL; - int date; - char* cbuf = (char*) xmalloc(SZ); - - cbuf[0] = '\0'; - if (t > 0) { - tm = localtime(&t); - if (tm) { - date = tm->tm_mday * 100000 + (1+tm->tm_mon) * 1000 + - tm->tm_year; - snprintf(cbuf, SZ, "%07d", date); - } - } - return cbuf; + QDate date = dt.date(); + int y = date.year() - 1900; + int m = date.month(); + int d = date.day(); + int r = d * 100000 + m * 1000 + y; + return QString("%1").arg(r, 7, 10, QChar('0')); } /* @@ -204,31 +191,14 @@ maggeo_fmtdate(time_t t) * century is three digits but anything from before 2000, we'd have * two digit years. This makes this easier to parse as strings. */ -static time_t maggeo_parsedate(char* dmy) +static QDateTime maggeo_parsedate(char* dmy) { - struct tm tm; - char dd[3]; - char mm[3]; - - if (strlen(dmy) < 5) { - return 0; - } - - memset(&tm, 0, sizeof(tm)); - - dd[0] = dmy[0]; - dd[1] = dmy[1]; - dd[2] = 0; - - mm[0] = dmy[2]; - mm[1] = dmy[3]; - mm[2] = 0; - - tm.tm_mday = atoi(dd); - tm.tm_mon = atoi(mm) - 1; - tm.tm_year = atoi(dmy + 4); - - return mktime(&tm); + QString date(dmy); + int d = date.mid(0,2).toInt(); + int m = date.mid(2,2).toInt(); + int y = date.mid(4,3).toInt(); + QDateTime r(QDate (y + 1900, m, d)); + return r; } /* @@ -273,8 +243,6 @@ maggeo_waypt_pr(const waypoint* waypointp) char* cname = NULL; const char* ctype = NULL; QString placer; - char* lfounddate = NULL; - char* placeddate = NULL; ilat = waypointp->latitude; ilon = waypointp->longitude; @@ -301,9 +269,10 @@ maggeo_waypt_pr(const waypoint* waypointp) } else { ctype = gs_get_cachetype(waypointp->gc_data->type); } - placeddate = maggeo_fmtdate(waypointp->creation_time); - lfounddate = maggeo_fmtdate(waypointp->gc_data->last_found); - cname = mkshort(desc_handle, waypointp->notes ? waypointp->notes : waypointp->description); + QString placeddate = maggeo_fmtdate(waypointp->creation_time); + QString lfounddate = maggeo_fmtdate(waypointp->gc_data->last_found); + cname = mkshort(desc_handle, + waypointp->notes ? waypointp->notes : waypointp->description); placer = waypointp->gc_data->placer; /* @@ -329,8 +298,8 @@ maggeo_waypt_pr(const waypoint* waypointp) append(obuf, placer.toUtf8().data()); append(obuf, waypointp->gc_data->hint.toUtf8().data()); append(obuf, ctype); - append(obuf, placeddate); - append(obuf, lfounddate); + append(obuf, placeddate.toUtf8()); + append(obuf, lfounddate.toUtf8()); if (waypointp->gc_data->diff/10.0) sprintf(obuf + strlen(obuf), ",%3.1f", @@ -346,12 +315,6 @@ maggeo_waypt_pr(const waypoint* waypointp) strcat(obuf, ","); } - if (lfounddate) { - xfree(lfounddate); - } - if (placeddate) { - xfree(placeddate); - } if (cname) { xfree(cname); } diff --git a/gpsbabel/mapasia.cc b/gpsbabel/mapasia.cc index 7c306a9fd..13caa3ade 100644 --- a/gpsbabel/mapasia.cc +++ b/gpsbabel/mapasia.cc @@ -85,14 +85,11 @@ tr7_read(void) while (! gbfeof(fin)) { unsigned char buff[TR7_S_SIZE]; double lat, lon; - struct tm tm; waypoint* wpt; float speed, course; gbfread(buff, 1, sizeof(buff), fin); - memset(&tm, 0, sizeof(tm)); - lat = (double)le_read32(&buff[TR7_S_LAT]) / 1000000.0; lon = (double)le_read32(&buff[TR7_S_LON]) / 1000000.0; @@ -101,17 +98,13 @@ tr7_read(void) continue; } - tm.tm_year = le_read16(&buff[TR7_S_YEAR]); - tm.tm_mon = buff[TR7_S_MONTH]; - tm.tm_mday = buff[TR7_S_DAY]; - tm.tm_hour = buff[TR7_S_HOUR]; - tm.tm_min = buff[TR7_S_MIN]; - tm.tm_sec = buff[TR7_S_SEC]; - - if ((tm.tm_mday < 1) || (tm.tm_mday > 31) || - (tm.tm_mon < 1) || (tm.tm_mon > 12) || - (tm.tm_year <= 1970) || - (tm.tm_year > tmref.tm_year+1)) { + QDate date(le_read16(&buff[TR7_S_YEAR]), + buff[TR7_S_MONTH], + buff[TR7_S_DAY]); + QTime time(buff[TR7_S_HOUR], + buff[TR7_S_MIN], + buff[TR7_S_SEC]); + if (!date.isValid() || !time.isValid()) { continue; } @@ -126,9 +119,7 @@ tr7_read(void) wpt->latitude = lat; wpt->longitude = lon; - tm.tm_year -= 1900; - tm.tm_mon -= 1; - wpt->creation_time = mkgmtime(&tm); + wpt->SetCreationTime(QDateTime(date, time, Qt::UTC)); WAYPT_SET(wpt, course, course); WAYPT_SET(wpt, speed, speed); @@ -240,16 +231,18 @@ tr7_disp_waypt_cb(const waypoint* wpt) le_write16(&buff[TR7_S_COURSE], (int)(360 - course)); } - if (wpt->creation_time) { - const time_t tt = wpt->creation_time; - tm = *gmtime(&tt); + QDateTime dt = wpt->GetCreationTime().toUTC(); + if (dt.isValid()) { + QDate d = dt.date(); + + le_write16(&buff[TR7_S_YEAR], d.year()); + buff[TR7_S_MONTH] = d.month(); + buff[TR7_S_DAY] = d.day(); - le_write16(&buff[TR7_S_YEAR], tm.tm_year + 1900); - buff[TR7_S_MONTH] = tm.tm_mon + 1; - buff[TR7_S_DAY] = tm.tm_mday; - buff[TR7_S_HOUR] = tm.tm_hour; - buff[TR7_S_MIN] = tm.tm_min; - buff[TR7_S_SEC] = tm.tm_sec; + QTime t = dt.time(); + buff[TR7_S_HOUR] = t.hour(); + buff[TR7_S_MIN] = t.minute(); + buff[TR7_S_SEC] = t.second(); if WAYPT_HAS(wpt, speed) { speed = wpt->speed; diff --git a/gpsbabel/mmo.cc b/gpsbabel/mmo.cc index cf91d906e..7ebb89d8b 100644 --- a/gpsbabel/mmo.cc +++ b/gpsbabel/mmo.cc @@ -173,7 +173,7 @@ static char* mmo_readstr(void) { char* res; - unsigned int len; + signed int len; len = (unsigned)gbfgetc(fin); if (len == 0xFF) { @@ -1270,7 +1270,7 @@ mmo_write_wpt_cb(const waypoint* wpt) int icon = 0; mmo_data_t* data; - time = wpt->creation_time; + time = wpt->GetCreationTime().toTime_t(); if (time < 0) { time = 0; } @@ -1378,9 +1378,9 @@ mmo_write_rte_head_cb(const route_head* rte) QUEUE_FOR_EACH(&rte->waypoint_list, elem, tmp) { waypoint* wpt = (waypoint*)elem; - - if ((wpt->creation_time > 0) && (wpt->creation_time < time)) { - time = wpt->creation_time; + QDateTime t = wpt->GetCreationTime(); + if ((t.isValid()) && (t.toTime_t() < time)) { + time = t.toTime_t(); } } if (time == 0x7FFFFFFF) { diff --git a/gpsbabel/pocketfms_bc.cc b/gpsbabel/pocketfms_bc.cc index 652193b9c..306d473b5 100644 --- a/gpsbabel/pocketfms_bc.cc +++ b/gpsbabel/pocketfms_bc.cc @@ -132,7 +132,7 @@ pocketfms_waypt_disp(const waypoint *wpt) struct tm *tm; memset(&bc, 0, sizeof(bc)); - const time_t tt = wpt->GetCreationTime(); + const time_t tt = wpt->GetCreationTime().toTime_t(); tm = localtime(&tt); if (wpt->creation_time) { const time_t tt = wpt->GetCreationTime(); diff --git a/gpsbabel/smplrout.cc b/gpsbabel/smplrout.cc index e0d49cd6f..2ff7a010c 100644 --- a/gpsbabel/smplrout.cc +++ b/gpsbabel/smplrout.cc @@ -187,7 +187,7 @@ compute_xte(struct xte *xte_rec) } // if timestamps exist, distance to interpolated point if (wpt1->GetCreationTime() != wpt2->GetCreationTime()) { - frac = (double)(wpt3->creation_time - wpt1->creation_time) / + frac = (double)(wpt3->GetCreationTime() - wpt1->GetCreationTime()) / (wpt2->GetCreationTime() - wpt1->GetCreationTime()); linepart(wpt1->latitude, wpt1->longitude, wpt2->latitude, wpt2->longitude, diff --git a/gpsbabel/waypt.cc b/gpsbabel/waypt.cc index 8dc79cfa6..85e03f6b0 100644 --- a/gpsbabel/waypt.cc +++ b/gpsbabel/waypt.cc @@ -235,7 +235,7 @@ set_waypt_count(unsigned int nc) void waypt_disp(const waypoint *wpt) { - if (wpt->creation_time.isValid()) { + if (wpt->GetCreationTime().isValid()) { printf("%s ", qPrintable(wpt->creation_time.toString())); } printposn(wpt->latitude,1); -- 2.30.2